﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using AutoDownload.Settings;
using Sabnzbd;

namespace AutoDownload.Config.Editor
{
    public partial class ConfigEditor : Form
    {
        private AutoDownload.Settings.Config m_ConfigFile;

        public AutoDownload.Settings.Config ConfigFile
        {
            get { return m_ConfigFile; }
            set { m_ConfigFile = value; }
        }

        private szdConfig m_sabConfig;

        public szdConfig SabConfig
        {
            get { return m_sabConfig; }
            set { m_sabConfig = value; }
        }

        public ConfigEditor(AutoDownload.Settings.Config configFile)
        {
            m_ConfigFile = configFile;
            InitializeComponent();
        }

        private void ConfigEditor_Load(object sender, EventArgs e)
        {
            if (!File.Exists(ConfigFile.SabnzbdINI))
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    ConfigFile.SabnzbdINI = openFileDialog1.FileName;
                }
                else
                {
                    this.Close();
                    return;
                }
            }

            configBindingSource.DataSource = ConfigFile;

            UpdateSabFeeds();

            tbEmailBody.Text = ConfigFile.EmailBody.Replace("\n",Environment.NewLine);
            btnShowAirOverview.Enabled = ConfigFile.SendOverview;
        }

        private void btnAddToMonitoredFeeds_Click(object sender, EventArgs e)
        {
            if (lbSabfeeds.SelectedItem != null)
            {
                ConfigFile.SabnzbdFeeds.Add( ((szdFeed)lbSabfeeds.SelectedItem).Name);
            }
            UpdateSabFeeds();
        }

        private void btnRemoveFromMonitoredFeeds_Click(object sender, EventArgs e)
        {
            if (lbMonitoredFeeds.SelectedItem != null)
            {
                sabnzbdFeedsBindingSource.RemoveCurrent();
            }
            UpdateSabFeeds();
        }

        private void UpdateSabFeeds()
        {
            try
            {
                SabConfig = SABnzbd.ParseINIFile(ConfigFile.SabnzbdINI);
            }
            catch
            {
                MessageBox.Show("Couldn't parse the sabnzbd ini file");
                this.Close();
                return;
            }

            //foreach (szdFeed szdFeed in SabConfig.Feeds)
            //{
            //    bool found = false;
            //    for (int j = ConfigFile.SabnzbdFeeds.Count - 1; j >= 0; j--)
            //    {
            //        if (szdFeed.Name.Equals(ConfigFile.SabnzbdFeeds[j]))
            //        {
            //            found = true;
            //        }
            //    }
            //    if (!found)
            //        ConfigFile.SabnzbdFeeds.RemoveAt(j);
            //}

            for (int j = ConfigFile.SabnzbdFeeds.Count - 1; j >= 0; j--)
            {
                string feed = ConfigFile.SabnzbdFeeds[j];  

                bool found = false;
                for (int i = SabConfig.Feeds.Count - 1; i >= 0; i--)
                {
                    if (SabConfig.Feeds[i].Name.Equals(feed))
                    {
                        SabConfig.Feeds.RemoveAt(i);
                        found = true;
                    }
                }

                if (!found)
                    ConfigFile.SabnzbdFeeds.RemoveAt(j);
            }

            szdConfigBindingSource.DataSource = SabConfig;
            configBindingSource.ResetBindings(false);
        }

        private void btnBrowseINI_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ConfigFile.SabnzbdINI = openFileDialog1.FileName;
            }
            else
            {
                this.Close();
                return;
            }
            
            configBindingSource.DataSource = ConfigFile;

            UpdateSabFeeds();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ConfigFile.EmailBody = tbEmailBody.Text.Replace(Environment.NewLine, "\n");
            ConfigFile.Save(AutoDownload.Settings.Config.GetConfigFileName());
            ReloadConfig = false;
            this.Close();
        }

        public bool ReloadConfig;

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ReloadConfig = true;
            this.Close();           
        }

        private void cbAirOverview_CheckedChanged(object sender, EventArgs e)
        {
            btnShowAirOverview.Enabled = cbAirOverview.Checked;
        }

        private void btnShowAirOverview_Click(object sender, EventArgs e)
        {
            DailyAirEditor _DAE = new DailyAirEditor(ConfigFile);
            _DAE.ShowDialog();
        }

        private void cbOverWriteRename_CheckedChanged(object sender, EventArgs e)
        {
            mtbRenameUpTo.Enabled = cbOverWriteRename.Checked;
            tbRename.Enabled = cbOverWriteRename.Checked;
        }
    }

}
